﻿
#include "CreateRectAngleDlg.h"
#include <vtkPolyLine.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreateRectangleDlg::CreateRectangleDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreateRectangleDlg::~CreateRectangleDlg()
{
}

bool CreateRectangleDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_point1Coord->getValues(m_point1[0], m_point1[1], m_point1[2]);
        m_point2Coord->getValues(m_point2[0], m_point2[1], m_point2[2]);
        m_point3Coord->getValues(m_point3[0], m_point3[1], m_point3[2]);

        double point4[3] = {0,0,0};

        double center[3] = {(m_point1[0] + m_point3[0]) * 0.5 ,
      (m_point1[1] + m_point3[1]) * 0.5,
      (m_point1[2] + m_point3[2]) * 0.5};

        point4[0] = (center[0] - m_point2[0]) * 2 + +m_point2[0];
        point4[1] = (center[1] - m_point2[1]) * 2 + +m_point2[1];
        point4[2] = (center[2] - m_point2[2]) * 2 + +m_point2[2];

        //create vtk polyline
        vtkNew<vtkPoints> points;
        points->InsertNextPoint(m_point1);
        points->InsertNextPoint(m_point2);
        points->InsertNextPoint(m_point3);
        points->InsertNextPoint(point4);
        points->InsertNextPoint(m_point1);

        vtkNew<vtkCellArray> lines;
        lines->InsertNextCell(5);
        lines->InsertCellPoint(0);
        lines->InsertCellPoint(1);
        lines->InsertCellPoint(2);
        lines->InsertCellPoint(3);
        lines->InsertCellPoint(4);

        vtkNew<vtkPolyData> polyData;
        polyData->SetPoints(points);
        polyData->SetLines(lines);

        //create vtk actor
        vtkNew<vtkPolyDataMapper> mapper;
        mapper->SetInputData(polyData);
        mapper->StaticOn();

        vtkNew<vtkActor> lineActor;
        lineActor->SetMapper(mapper);
        lineActor->GetProperty()->SetColor(QtVtkModle::Global::RectangleColor[0], QtVtkModle::Global::RectangleColor[1], QtVtkModle::Global::RectangleColor[2]);

        //add line to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(lineActor);
        GetActorManagerInstance()->AddActor(lineActor, PickMode::EDGE);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreateRectangleDlg::initUI()
{
    setWindowTitle("Create Rectangle");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_point1Coord = new MyCoordinateEdit(this, "Point 1");
        pDlgLayout->addWidget(m_point1Coord);

        m_point2Coord = new MyCoordinateEdit(this, "Point 2");
        m_point2Coord->setValues(1, 0, 0);
        pDlgLayout->addWidget(m_point2Coord);

        m_point3Coord = new MyCoordinateEdit(this, "Point 3");
        m_point3Coord->setValues(1, 1, 0);
        pDlgLayout->addWidget(m_point3Coord);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreateRectangleDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create rectangle!");
                }

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreateRectangleDlg::close);
}



